class Solution:
    def longestNiceSubstring(self, s: str) -> str:
        ans = ""

        for i in range(len(s)):
            contain = set()
            need = set()
            for j in range(i, len(s)):
                if s[j].lower() not in contain:
                    if s[j] in need:
                        need.remove(s[j])
                        contain.add(s[j].lower())
                    else:
                        if s[j].islower():
                            need.add(s[j].upper())
                        else:
                            need.add(s[j].lower())
                if len(need) == 0:
                    if j - i + 1 > len(ans):
                        ans = s[i:j + 1]

        return ans


if __name__ == "__main__":
    print(Solution().longestNiceSubstring("YazaAay"))  # aAa
    print(Solution().longestNiceSubstring("Bb"))  # Bb
    print(Solution().longestNiceSubstring("c"))  # ""
    print(Solution().longestNiceSubstring("dDzeE"))  # dD
